/**
 * Copyright (c) 2011 Nokia Corporation.
 */

import QtQuick 1.0
import GEAudio 1.0

AppWindow {
    id: window
    anchors.fill: parent

    AudioMixer {
        id: mymixer
        absoluteVolume: 0.5
        enabled: true
        effect: [
            EchoEffect {
                id: masterEcho
                delay: 0.5
                decay: 0.0
            },
            CutOffEffect {
                id: masterCutoff
                cutOff: 1.0
                resonance: 1.0
            }
        ]

        AudioBuffer {
            id: sampleA
            source: ":/audio/ivory.wav"
            effect: [
                EchoEffect {
                    id: sampleAecho
                    delay: 0.5
                    decay: 0.0
                },
                CutOffEffect {
                    id: sampleAcutoff
                    cutOff: 1.0
                    resonance: 1.0
                }
            ]
            onPlaybackStopped:
                playControls.updateState();
        }

        AudioBuffer {
            id: sampleB
            source: ":/audio/boing.wav"
            effect: [
                EchoEffect {
                    id: sampleBecho
                    delay: 0.5
                    decay: 0.0
                },
                CutOffEffect {
                    id: sampleBcutoff
                    cutOff: 1.0
                    resonance: 1.0
                }
            ]
            onPlaybackStopped:
                playControls.updateState();
        }
    }

    Image {
        id: win
        anchors.fill: parent
        source: "qrc:/gfx/bg.png"
        smooth: true
        fillMode: Image.Stretch
        property int buttonWidth: win.width / 3
        property int buttonHeight: win.height / 5
        property int sliderHeight: (win.height - 30 * 2 - buttonHeight * 2) / 5

        Box {
            id: mixerBox
            width: win.buttonWidth;
            height: win.buttonHeight;
            x: 0; y: 30
            selected_image: "qrc:/gfx/selected_mixer_button.png"
            unselected_image: "qrc:/gfx/mixer_button.png"

            MouseArea {
                anchors.fill: parent
                onPressed: mixerBox.setAsActive();
            }

            function setAsActive()
            {
                samplePlayerA.unselect();
                samplePlayerB.unselect();

                masterVolumeSlider.value = mymixer.absoluteVolume;
                pitchSlider.value = 1.0;
                resonanceSlider.value = masterCutoff.resonance;
                cutoffSlider.value = masterCutoff.cutOff;
                echoSlider.value = masterEcho.decay;

                mixerBox.select();
                playControls.updateState();
            }
        }

        Box {
            id: samplePlayerA
            x: win.buttonWidth; y: 30
            width: win.buttonWidth;
            height: win.buttonHeight;
            selected_image: "qrc:/gfx/selected_sample_a_button.png"
            unselected_image: "qrc:/gfx/sample_a_button.png"

            MouseArea {
                anchors.fill: parent
                onPressed: samplePlayerA.setAsActive();
            }

            function setAsActive()
            {
                if (!samplePlayerA.selected) {
                    mixerBox.unselect();
                    samplePlayerB.unselect();

                    masterVolumeSlider.value = sampleA.volume;
                    pitchSlider.value = sampleA.speed;
                    echoSlider.value = sampleAecho.decay;
                    resonanceSlider.value = sampleAcutoff.resonance;
                    cutoffSlider.value = sampleAcutoff.cutOff;

                    samplePlayerA.select();
                } else
                    sampleA.play();

                playControls.updateState();
            }
        }

        Box {
            id: samplePlayerB
            x: win.buttonWidth * 2; y: 30
            width: win.buttonWidth;
            height: win.buttonHeight;
            selected_image: "qrc:/gfx/selected_sample_b_button.png"
            unselected_image: "qrc:/gfx/sample_b_button.png"

            MouseArea {
                anchors.fill: parent
                onPressed: samplePlayerB.setAsActive();
            }

            function setAsActive()
            {
                if (!samplePlayerB.selected) {
                    mixerBox.unselect();
                    samplePlayerA.unselect();

                    masterVolumeSlider.value = sampleB.volume;
                    pitchSlider.value = sampleB.speed;
                    echoSlider.value = sampleBecho.decay;
                    resonanceSlider.value = sampleBcutoff.resonance;
                    cutoffSlider.value = sampleBcutoff.cutOff;

                    samplePlayerB.select();
                } else
                    sampleB.play();

                playControls.updateState();
            }
        }

        Item {
            id: playControls;

            Box {
                id: playBox
                x: 0
                y: win.height - win.buttonHeight - 30
                width: win.buttonWidth
                height: win.buttonHeight
                selected_image: "qrc:/gfx/stop_button.png"
                unselected_image: "qrc:/gfx/play_button.png"
                disabled_image: "qrc:/gfx/disable_play_button.png"

                MouseArea {
                    anchors.fill: parent
                    onPressed:  {
                        if (samplePlayerA.selected) {
                            if (sampleA.playing())
                                sampleA.stop();
                            else
                                sampleA.play();
                            playControls.updateState();
                        } else if (samplePlayerB.selected) {
                            if (sampleB.playing())
                                sampleB.stop();
                            else
                                sampleB.play();
                            playControls.updateState();
                        }
                    }
                }
            }

            Box {
                id: repeatBox
                x: win.buttonWidth
                y: win.height - win.buttonHeight - 30
                width: win.buttonWidth;
                height: win.buttonHeight;
                selected_image: "qrc:/gfx/disable_repeat_button.png"
                unselected_image: "qrc:/gfx/play_repeat.png"
                disabled_image: "qrc:/gfx/disable_repeat_button.png"

                MouseArea {
                    anchors.fill: parent
                    onPressed: {
                        if (samplePlayerA.selected) {
                            if (sampleA.loopCount == 0)
                                sampleA.loopCount = 65536;
                            else
                                sampleA.loopCount = 0;
                        } else if (samplePlayerB.selected) {
                            if (sampleB.loopCount == 0)
                                sampleB.loopCount = 65536;
                            else
                                sampleB.loopCount = 0;
                        }

                        playControls.updateState();
                    }
                }
            }

            function updateState() {
                if ((samplePlayerA.selected && sampleA.playing()) ||
                    (samplePlayerB.selected && sampleB.playing())) {
                    playBox.select();
                } else {
                    playBox.unselect();
                }

                if ((samplePlayerA.selected && sampleA.loopCount != 0) ||
                    (samplePlayerB.selected && sampleB.loopCount != 0))
                    repeatBox.select();
                else
                    repeatBox.unselect();

                if (mixerBox.selected) {
                    playBox.disable();
                    repeatBox.disable();
                    pitchSlider.enabled = false;
                    pitchSlider.source = "qrc:/gfx/disable_pitch_line.png"
                } else {
                    pitchSlider.enabled = true;
                    pitchSlider.source = "qrc:/gfx/pitch_line.png"
                }
            }

        }

        Box {
            id: exitBox
            x: win.buttonWidth * 2
            y: win.height - win.buttonHeight - 30
            width: win.buttonWidth;
            height: win.buttonHeight;
            source: "qrc:/gfx/exit_button.png"

            MouseArea {
                anchors.fill: parent
                onPressed: Qt.quit();
            }
        }

        Slider {
            height: win.sliderHeight
            y: win.buttonHeight + 30;
            id: masterVolumeSlider
            source: "qrc:/gfx/volume_line.png"
            label: "Volume"
        }

        Slider {
            height: win.sliderHeight
            y: masterVolumeSlider.y + win.sliderHeight
            id: pitchSlider
            minval: 0; maxval: 2;
            source: "qrc:/gfx/pitch_line.png"
            label: "Pitch"
        }

        Slider {
            height: win.sliderHeight
            y: pitchSlider.y + win.sliderHeight
            id: cutoffSlider
            minval: 1; maxval: 0;
            source: "qrc:/gfx/cut_off_line.png"
            label: "Cut off"
        }

        Slider {
            height: win.sliderHeight
            y: cutoffSlider.y + win.sliderHeight
            minval: 1; maxval: 0;
            id: resonanceSlider
            source: "qrc:/gfx/resonance_line.png"
            label: "Resonance"
        }

        Slider {
            height: win.sliderHeight
            y: resonanceSlider.y + win.sliderHeight
            id: echoSlider
            source: "qrc:/gfx/echo_line.png"
            label: "Echo"
        }
    }

    Binding {
        target: mymixer; property: "absoluteVolume"
        value: masterVolumeSlider.value; when: mixerBox.selected
    }

    Binding {
        target: sampleA; property: "volume"
        value: masterVolumeSlider.value; when: samplePlayerA.selected
    }

    Binding {
        target: sampleB; property: "volume"
        value: masterVolumeSlider.value; when: samplePlayerB.selected
    }

    Binding {
        target: sampleA; property: "speed"
        value: pitchSlider.value; when: samplePlayerA.selected
    }

    Binding {
        target: sampleB; property: "speed"
        value: pitchSlider.value; when: samplePlayerB.selected
    }

    Binding {
        target: masterCutoff; property: "cutOff"
        value: cutoffSlider.value; when: mixerBox.selected
    }

    Binding {
        target: sampleAcutoff; property: "cutOff"
        value: cutoffSlider.value; when: samplePlayerA.selected
    }

    Binding {
        target: sampleBcutoff; property: "cutOff"
        value: cutoffSlider.value; when: samplePlayerB.selected
    }

    Binding {
        target: masterCutoff; property: "resonance"
        value: resonanceSlider.value; when: mixerBox.selected
    }

    Binding {
        target: sampleAcutoff; property: "resonance"
        value: resonanceSlider.value; when: samplePlayerA.selected
    }

    Binding {
        target: sampleBcutoff; property: "resonance"
        value: resonanceSlider.value; when: samplePlayerB.selected
    }

    Binding {
        target: masterEcho; property: "decay"
        value: echoSlider.value; when: mixerBox.selected
    }

    Binding {
        target: sampleAecho; property: "decay"
        value: echoSlider.value; when: samplePlayerA.selected
    }

    Binding {
        target: sampleBecho; property: "decay"
        value: echoSlider.value; when: samplePlayerB.selected
    }

    Component.onCompleted: {
        mixerBox.setAsActive();
    }
}
